package com.evaluationsystem.system.serivce.impl;

import cn.hutool.core.date.DateUtil;
import com.evaluationsystem.Utils.ModularNameUtils;
import com.evaluationsystem.Utils.UserContextUtils;
import com.evaluationsystem.system.dao.DictionaryDao;
import com.evaluationsystem.system.dao.PositionAndTeamDao;
import com.evaluationsystem.system.entity.Dictionary;
import com.evaluationsystem.system.entity.Tlogs;
import com.evaluationsystem.system.entity.UserInfo;
import com.evaluationsystem.system.serivce.PositionAndTeamService;
import com.evaluationsystem.system.serivce.TlogsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PositionAndTeamServiceImpl implements PositionAndTeamService {

    @Autowired
    PositionAndTeamDao dao;

    @Autowired
    DictionaryDao dictionaryDao;

    @Autowired
    UserContextUtils userContextUtils;

    @Autowired
    TlogsService logsService;


    @Override
    public Map<String, Object> getPositionTableList(Map<String, Object> param) {
        Map<String,Object> map = new HashMap<>();
        System.out.println(dao.selectPositionAll(param));
        PageHelper.startPage(Integer.parseInt(param.get("page").toString()), Integer.parseInt(param.get("limit").toString()));
        List<Map<String, Object>> positions =  dao.selectPositionAll(param);
        PageInfo<Map<String, Object>> PageInfos = new PageInfo<>(positions);
        map.put("code","0");//解析接口状态
        map.put("msg","success");//解析提示文本
        map.put("count",PageInfos.getTotal());//解析数据长度
        map.put("data",PageInfos.getList());//解析数据列表
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> positionAdd(Map<String, Object> param) {
        /*{vPositionName=asdasd}*/
        Map<String,Object> res = new HashMap<>();
        param.put("addAndUpata","");
        if (StringUtils.isNotEmpty(dao.selectPositionByPositionName(param))){
            res.put("code",5001);
            res.put("msg","职位名称已存在");
            return res;
        }
        /*ZW0001开始自增长*/
        param.put("vPositionId",userContextUtils.getMaxid(dao.MaxPositionId(),"ZW",4,"0"));
        param.put("vInsUserID",userContextUtils.getUser().getUserid());
        param.put("vInsDate", DateUtil.today());
        try {
            int a = dao.positionAdd(param);
            a = logsService.add(new Tlogs()
                    .setVoperationcontent(param.get("vPositionName").toString())/*操作内容*/
                    .setVmodularname(ModularNameUtils.PositionAdd)/*操作模块名称*/
                    .setVpagename(ModularNameUtils.Position)/*操作页面名称*/
                    .setVoperation(userContextUtils.getUserId())/*操作人*/
                    .setVrecordscode(param.get("vPositionId").toString())
            );
            res.put("code",200);
            res.put("msg","添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            res.put("code",5002);
            res.put("msg","数据异常");
        }
        return res;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> updataPositionVpositionname(Map<String, Object> param) {
        Map<String,Object> res = new HashMap<>();
        param.put("vupduserid",userContextUtils.getUserId());
        param.put("vupddate",DateUtil.today());
        /*System.out.println("updataPositionVpositionname"+param);*/
        param.put("addAndUpata","1");
        if (StringUtils.isNotEmpty(dao.selectPositionByPositionName(param))){
            res.put("code",5001);
            res.put("msg","职位名称已存在");
            return res;
        }
        try {
            int a = dao.UpdataPositionVpositionname(param);
            a = logsService.add(new Tlogs()
                    .setVoperationcontent(param.get("vPositionName").toString())/*操作内容*/
                    .setVmodularname(ModularNameUtils.PositionUpdata)/*操作模块名称*/
                    .setVpagename(ModularNameUtils.Position)/*操作页面名称*/
                    .setVoperation(userContextUtils.getUserId())/*操作人*/
                    .setVrecordscode(param.get("vpositionid").toString())
            );
            res.put("code",200);
            res.put("msg","修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            res.put("code",5001);
            res.put("msg","修改失败，联系管理员");
        }
        return res;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> startAndForbid(Map<String, Object> param) {
        /*{vpositionid=ZW0001, vpositionname=1, vupddate=, vinsdate=2020-04-14, vupduserid=, vinsuserid=emp, del_flg=0, startAndForbid=forbid}*/
        Map<String,Object> res = new HashMap<>();
        param.put("vupduserid",userContextUtils.getUserId());
        param.put("vupddate",DateUtil.today());
        try {
            int a = dao.startAndForbid(param);
            a = logsService.add(new Tlogs()
                    .setVoperationcontent(param.get("startAndForbid").toString())/*操作内容*/
                    .setVmodularname(ModularNameUtils.PositionUpdata)/*操作模块名称*/
                    .setVpagename(ModularNameUtils.Position)/*操作页面名称*/
                    .setVoperation(userContextUtils.getUserId())/*操作人*/
                    .setVrecordscode(param.get("vpositionid").toString())
            );
            res.put("code",200);
            res.put("msg","修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            res.put("code",5001);
            res.put("msg","修改失败，联系管理员");
        }
        return res;
    }

    @Override
    public Map<String, Object> getTeamTableList(Map<String, Object> param) {
        /*page=1, limit=10, vteamname=asdasd, vAppraiseFlg=, vFactoryFlg=*/
        System.out.println(param);
        Map<String,Object> map = new HashMap<>();
        PageHelper.startPage(Integer.parseInt(param.get("page").toString()), Integer.parseInt(param.get("limit").toString()));
        List<Map<String, Object>> positions =  dao.selectTteamInfoAll(param);
        PageInfo<Map<String, Object>> PageInfos = new PageInfo<>(positions);
        map.put("code","0");//解析接口状态
        map.put("msg","success");//解析提示文本
        map.put("count",PageInfos.getTotal());//解析数据长度
        map.put("data",PageInfos.getList());//解析数据列表
        return map;
    }

    @Override
    public Map<String, Object> radioAndAelectData() {
        Map<String,Object> map = new HashMap<>();
        List<Dictionary> factorys = dictionaryDao.selByType("001");
        List<Dictionary> appraises = dictionaryDao.selByType("002");
        map.put("factorys",factorys);
        map.put("appraises",appraises);
        map.put("code",200);
        map.put("msg","ok");
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> teamAdd(Map<String, Object> param) {
        Map<String,Object> map = new HashMap<>();
        if(!StringUtils.isEmpty(dao.selTeamNameByvTeamName(param))){
            map.put("code",5001);
            map.put("msg","班组名称已存在");
            return map;
        }
        /*班组编号(BZ0001开始自增长）*/
        param.put("vteamid",userContextUtils.getMaxid(dao.selTeamMaxId(),"BZ",4,"0"));
        param.put("vinsuserid",userContextUtils.getUserId());
        param.put("vinsdate",DateUtil.today());

        try {
            int a = dao.insertTeam(param);
            a = logsService.add(new Tlogs()
                .setVoperationcontent(param.get("vteamname").toString())/*操作内容*/
                .setVmodularname(ModularNameUtils.TeamInfoAdd)/*操作模块名称*/
                .setVpagename(ModularNameUtils.TeamInfo)/*操作页面名称*/
                .setVoperation(userContextUtils.getUserId())/*操作人*/
                .setVrecordscode(param.get("vteamid").toString())
            );
            map.put("code",200);
            map.put("msg","添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            map.put("code",5001);
            map.put("msg","数据访问异常，请联系管理员");
        }
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> teamStartAndForbid(Map<String, Object> param) {
        /*{vteamid=BZ0003, vupddate=2020-04-15, vfactoryflg=1, vinsdate=2020-04-15, vupduserid=gdy,
        vfactoryflgname=迈纳拓, vinsuserid=emp, del_flg=1,
        del_flgname=启用, vteamname=223, vappraiseflgname=是, vappraiseflg=1, startAndForbid=forbid}*/
        Map<String,Object> map = new HashMap<>();
        try {
            param.put("vupduserid",userContextUtils.getUserId());
            param.put("vupddate",DateUtil.today());
            int a = dao.upTeamDelFlgByTeamid(param);
            a = logsService.add(new Tlogs()
                    .setVoperationcontent(param.get("startAndForbid").toString())/*操作内容*/
                    .setVmodularname(ModularNameUtils.TeamInfoUpdata)/*操作模块名称*/
                    .setVpagename(ModularNameUtils.TeamInfo)/*操作页面名称*/
                    .setVoperation(userContextUtils.getUserId())/*操作人*/
                    .setVrecordscode(param.get("vteamid").toString())
            );
            map.put("code",200);
            map.put("msg","修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            map.put("code",5001);
            map.put("msg","数据访问异常，请联系管理员");
        }
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> teamUpdata(Map<String, Object> param) {
        Map<String,Object> map = new HashMap<>();

        try {
            if(!StringUtils.isEmpty(dao.upselTeamNameByvTeamName(param))){
                map.put("code",5001);
                map.put("msg","班组名称已存在");
                return map;
            }
            int a = dao.delTeamInfoByTeamid(param);
            param.put("vupduserid",userContextUtils.getUserId());
            param.put("vupddate",DateUtil.today());
            a = dao.upinsertTeam(param);
            a = logsService.add(new Tlogs()
                    .setVoperationcontent(param.get("vTeamName").toString())/*操作内容*/
                    .setVmodularname(ModularNameUtils.TeamInfoUpdata)/*操作模块名称*/
                    .setVpagename(ModularNameUtils.TeamInfo)/*操作页面名称*/
                    .setVoperation(userContextUtils.getUserId())/*操作人*/
                    .setVrecordscode(param.get("vteamid").toString())
            );
            map.put("code",200);
            map.put("msg","修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            map.put("code",5001);
            map.put("msg","数据访问异常，请联系管理员");
        }
        return map;
    }
}
